package com.ho1ho.phonevolume;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.os.Bundle;
import android.widget.Toast;

import static android.media.AudioManager.FLAG_PLAY_SOUND;
import static android.media.AudioManager.STREAM_ALARM;
import static android.media.AudioManager.STREAM_MUSIC;
import static android.media.AudioManager.STREAM_NOTIFICATION;
import static android.media.AudioManager.STREAM_RING;
import static android.media.AudioManager.STREAM_SYSTEM;

public class MainAliasActivity extends Activity {

    private static final float VOLUME_PERCENT = 0.15f;
    private static final float NOTIFICATION_PERCENT = 0.1f;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences sp = getSharedPreferences("ho1ho_phonevolume", MODE_PRIVATE);
        int savedVolType = sp.getInt("curVolType", 1);
        int curVolType = (savedVolType + 1) % 2;
        sp.edit().putInt("curVolType", curVolType).apply();


        AudioManager am = (AudioManager) getSystemService(Service.AUDIO_SERVICE);

        int maxAlarmVolume = am.getStreamMaxVolume(AudioManager.STREAM_ALARM);
        int maxMusicVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int maxNotificationVolume = am.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
        int maxRingVolume = am.getStreamMaxVolume(AudioManager.STREAM_RING);
        int maxSystemVolume = am.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
//        int maxVoiceCallVolume = am.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
//        Toast.makeText(this, "Init maxVoiceCallVolume: " + maxVoiceCallVolume, Toast.LENGTH_SHORT).show();

        if (curVolType == 0) {
            // Indoor
            Toast.makeText(this, "Indoor Mode", Toast.LENGTH_SHORT).show();
            am.setStreamVolume(STREAM_ALARM, (int) Math.floor(maxAlarmVolume * VOLUME_PERCENT), FLAG_PLAY_SOUND);
            am.setStreamVolume(STREAM_MUSIC, (int) Math.floor(maxMusicVolume * VOLUME_PERCENT), FLAG_PLAY_SOUND);
            am.setStreamVolume(STREAM_NOTIFICATION, (int) Math.floor(maxNotificationVolume * NOTIFICATION_PERCENT), FLAG_PLAY_SOUND);
            am.setStreamVolume(STREAM_RING, (int) Math.floor(maxRingVolume * VOLUME_PERCENT), FLAG_PLAY_SOUND);
            am.setStreamVolume(STREAM_SYSTEM, (int) Math.floor(maxSystemVolume * VOLUME_PERCENT), FLAG_PLAY_SOUND);
//            am.setStreamVolume(STREAM_VOICE_CALL, (int) Math.floor(maxVoiceCallVolume * VOLUME_PERCENT), FLAG_PLAY_SOUND);
//            Toast.makeText(this, "current call volume: " + ((int) Math.floor(maxVoiceCallVolume * VOLUME_PERCENT)), Toast.LENGTH_SHORT).show();
//        am.setStreamVolume(STREAM_DTMF, curVolume, FLAG_PLAY_SOUND);

            PackageManager pm = getPackageManager();
            pm.setComponentEnabledSetting(
                    getComponentName(),
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP);
            pm.setComponentEnabledSetting(
                    new ComponentName(this, "com.ho1ho.phonevolume.MainActivity"),
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);
        } else {
            // Outdor Mode
            Toast.makeText(this, "Outdoor Mode", Toast.LENGTH_SHORT).show();
            am.setStreamVolume(STREAM_ALARM, maxAlarmVolume, FLAG_PLAY_SOUND);
            am.setStreamVolume(STREAM_MUSIC, maxMusicVolume, FLAG_PLAY_SOUND);
            am.setStreamVolume(STREAM_NOTIFICATION, maxNotificationVolume, FLAG_PLAY_SOUND);
            am.setStreamVolume(STREAM_RING, maxRingVolume, FLAG_PLAY_SOUND);
            am.setStreamVolume(STREAM_SYSTEM, maxSystemVolume, FLAG_PLAY_SOUND);
//            am.setStreamVolume(STREAM_VOICE_CALL, maxVoiceCallVolume, FLAG_PLAY_SOUND);
//            Toast.makeText(this, "maxVoiceCallVolume: " + maxVoiceCallVolume, Toast.LENGTH_SHORT).show();
    //        am.setStreamVolume(STREAM_DTMF, curVolume, FLAG_PLAY_SOUND);

            PackageManager pm = getPackageManager();
            pm.setComponentEnabledSetting(
                    getComponentName(),
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP);
            pm.setComponentEnabledSetting(
                    new ComponentName(this, "com.ho1ho.phonevolume.MainAliasActivity"),
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);
        }


        finish();
    }
}
